﻿using System;
using Castle.DynamicProxy;

namespace Accona.Remoting.Remoting
{
    public class RemoteObjectProxyFactory
    {
        private readonly ISender sender;
        private readonly Guid senderId;
        private readonly ServiceLookup serviceLookup;
        private readonly WaiterQueue waiterQueue;

        public RemoteObjectProxyFactory(Guid appId, WaiterQueue waiterQueue, ISender sender, ServiceLookup serviceLookup)
        {
            senderId = appId;
            this.serviceLookup = serviceLookup;
            this.waiterQueue = waiterQueue;
            this.sender = sender;
        }

        public T CreateProxy<T>()
        {
            var generator = new ProxyGenerator();
            var proxy =
                (T)
                generator.CreateInterfaceProxyWithoutTarget(typeof (T),
                                                            new RemoteCallInterceptor(senderId, sender, waiterQueue,
                                                                                      serviceLookup));
            return proxy;
        }
    }
}